package org.hackystat.ui.telemetryviewer.gwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * The custom widget for the JFCS service testing.
 *
 * @author Pavel Senin.
 *
 */
public class JFCSServiceStaticTst
    extends ChartWidget {

  private final ChartServiceRPCAsync chartService;
  private final ServiceDefTarget endpoint;
  private final String moduleRelativeURL;

  // final JFCSImageWidget chartImage;
  final Image chartImage;

  static Label chartLabel = new Label("Dummy debug: ");

  Widget OldSelectionHolder;
  static final String itemStyle = "spanRange-Item";
  static final String selectedItemStyle = "spanRange-Item-selected";

  static final String trickyStyleName = "chart-SpanWidget";

  /**
   * Creates an instance and reports a handler.
   *
   * @return the widget handler.
   */
  public static ChartHandler init() {
    return new ChartHandler("JFCSServiceStaticTest", "JFCS service test") {
      public ChartWidget createInstance() {
        return new JFCSServiceStaticTst();
      }
    };
  }

  /**
   * Creates the object and initializes all of the internal widgets.
   *
   */
  public JFCSServiceStaticTst() {

    /*
     * Setup connection to chart generator service.
     */
    chartService = (ChartServiceRPCAsync) GWT.create(ChartServiceRPC.class);
    endpoint = (ServiceDefTarget) chartService;
    moduleRelativeURL = GWT.getModuleBaseURL() + "chartService";
    endpoint.setServiceEntryPoint(moduleRelativeURL);

    /*
     * add the chart legend at the top
     */
    HTML description = new HTML(
        "This is a synthetic test of JFCS service. You are able to switch between two charts which "
            + "are pre-generated and hosted at the RESTlet based server. To switch between "
            + "charts use buttons.");
    description.setStyleName("chart-InfoWidget");

    /*
     * add the buttons for static image selection
     */
    HorizontalPanel buttonsPanel = new HorizontalPanel();
    buttonsPanel.setStyleName("chartSpan-Control");
    HorizontalPanel buttonsHolder = new HorizontalPanel();
    buttonsHolder.setStyleName("spanRange-List");
    buttonsPanel.add(buttonsHolder);

    Button button1 = new Button("jfreechart-12434.png");
    // Hook up a listener to find out when it's clicked.
    button1.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        update("languages");
        chartImage.setUrl("http://localhost:3128/jfcs/file/jfreechart-12434.png");
        sender.setStyleName(selectedItemStyle);
        if (null != OldSelectionHolder) {
          OldSelectionHolder.setStyleName(itemStyle);
        }
        OldSelectionHolder = sender;
      }
    });
    button1.setStyleName("spanRange-Item");
    buttonsHolder.add(button1);

    Button button2 = new Button("jfreechart-12435.png");
    // Hook up a listener to find out when it's clicked.
    button2.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        update("dependency");
        chartImage.setUrl("http://localhost:3128/jfcs/file/jfreechart-12435.png");
        sender.setStyleName(selectedItemStyle);
        if (null != OldSelectionHolder) {
          OldSelectionHolder.setStyleName(itemStyle);
        }
        OldSelectionHolder = sender;
      }
    });
    button2.setStyleName("spanRange-Item");
    buttonsHolder.add(button2);

    /*
     * finally add image widget to here
     */
    chartImage = new Image();
    chartImage.setStyleName("chart-Image");

    /*
     * and add the dummy debugG
     */
    chartLabel.setStyleName("dummyDebugLabel");
    HorizontalPanel hpDebugLabel = new HorizontalPanel();
    hpDebugLabel.add(chartLabel);
    hpDebugLabel.setStyleName(trickyStyleName);

    /* adding all this stuff to the root panel */
    VerticalPanel mainLayoutPanel = new VerticalPanel();
    mainLayoutPanel.setVerticalAlignment(VerticalPanel.ALIGN_TOP);
    mainLayoutPanel.setSpacing(0);
    mainLayoutPanel.setStyleName("chart-DependencyPanel");

    mainLayoutPanel.add(description);

    VerticalPanel allWidgetsPanel = new VerticalPanel();
    allWidgetsPanel.setStyleName(trickyStyleName);
    allWidgetsPanel.add(buttonsPanel);
    allWidgetsPanel.add(chartImage);
    allWidgetsPanel.add(chartLabel);

    mainLayoutPanel.add(allWidgetsPanel);

    initWidget(mainLayoutPanel);

  }

  /**
   * Does the actual RPC call to the server in order get the new chart filename.
   *
   * @param type the type of plot we are interested.
   */
  public void update(String type) {

    /*
     * Setup the callback from the chart generator service.
     */
    AsyncCallback callback = new AsyncCallback() {
      /*
       * If the call was successful, we will get back the name of the chart that was created and
       * stored on the server.
       */
      public void onSuccess(Object s) {
        String chartName = (String) s;
        chartLabel.setText("Dummy debug: current request URI is " + chartName);
      }

      /*
       * Something went wrong with the call. Handle the issue how you'd like.
       */
      public void onFailure(Throwable ex) {
        // do nothing for now
      }
    };

    /*
     * Make the call to the chart generator service with the previously created callback.
     */
    chartService.generateChart(type, callback);

    /*
     * Since we've made an asynchronous call, we don't need to do anything further. The callback
     * object will handle the remainder of the work for us.
     */
  }
}
